<template>
	<layout>
		<!-- #ifdef MP-WEIXIN -->
		<uv-navbar
		  :fixed="false"
		  :title="title"
		  left-arrow
		  @leftClick="$onClickLeft"
		/>
		<!-- #endif -->
		<view class="wrap">
			<view class="content">
				<view class="title">沉就CRM</view>
				<view class="font-size-base text-center text-color-assist mt-1 mb-5 ">实现线索到回款全流程管理</view>
				
				<view class="text-color-assist mb-2">账户</view>
				<uv-input placeholder="请输入账户" prefixIcon="account" border="bottom" v-model="username"></uv-input>
				<view class="text-color-assist mt-5 mb-2">密码</view>
				<uv-input placeholder="请输入密码" type="password" prefixIcon="lock" border="bottom" v-model="password"></uv-input>
				
				<button @tap="submit" type="primary" class="login">账户登录</button>
				<!-- #ifdef MP-WEIXIN -->
				<button @click="workWxLogin" type="primary" class="login1">企微登录</button>
				<!-- #endif -->
			</view>
		</view>
		<uv-toast ref="uToast"></uv-toast>
	</layout>
</template>

<script setup>
import {
  ref,
  computed
} from 'vue'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { useMainStore } from '@/store/store'
import {
  userLogin,
  wxLogin
} from '@/api/auth'
import * as util from '@/utils/util'
import { mobile as testMobible } from '@/uni_modules/uv-ui-tools/libs/function/test'
import cookie from '@/utils/cookie'
import router from '@/utils/router'

const main = useMainStore()
const title = ref('登录')
const username = ref('')
const password = ref('')
const isChecked = ref(false)
const openid = ref(main.openid)
const uToast = ref()

onShow(() => {
	// #ifdef MP-WEIXIN
	if (!openid.value) {
		//wechatMiniLogin();
	}
	// #endif
})

const check = () => {
	if (!isChecked.value) {
		uToast.value.show({
			message: '请勾选下面协议',
			type: 'error'
		});
		return
	}
}

// 提交
const submit = () => {
	if (username.value == '') {
		uToast.value.show({
			message: '请填写账户',
			type: 'error'
		});
		return
	}
	
	if (password.value == '') {
		uToast.value.show({
			message: '请填写密码',
			type: 'error'
		});
		return
	}
	
	login()
}

// 登录
const login = async () => {
	let data = await userLogin({
		username: username.value,
		password: password.value,
		socialType: 34
	})
	if (data) {
		uni.setStorage({
			key: 'accessToken',
			data: data.accessToken
		});
		main.SET_TOKEN(data.accessToken);
		main.SET_MEMBER(data)
		uToast.value.show({
			message: '登录成功',
			type: 'success'
		});
		
		setTimeout(function() {
			uni.switchTab({
				url: '/pages/index/index'
			})
		}, 2000);
	}
}

//企业微信登录
const workWxLogin = () => {
	if (!wx.qy) {
		uToast.value.show({
			message: '企业微信中打开才能登录哦！',
			type: 'error'
		});
		return
	}
	wx.qy.login({
		success: async function(res) { 
			if (res.code) {
				let data = await wxLogin({
					code: res.code
				})
				if (data) {
					uni.setStorage({
						key: 'accessToken',
						data: data.accessToken
					});
					main.SET_TOKEN(data.accessToken);
					uToast.value.show({
						message: '登录成功',
						type: 'success'
					});
					
					setTimeout(function() {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}, 2000);
				}
			}
		}
	});
}

const onChange = () => {
	isChecked.value = !isChecked.value
}
</script>

<style lang="scss" scoped>
.wrap {
	background-color: #ffffff;
	font-size: 28rpx;
	height: 100vh; /* 使用 vh 更可靠 */
	display: flex;
	flex-direction: column;
	justify-content: center; /* 垂直居中关键 */
	align-items: stretch; /* 确保子元素可以居中 */
	padding-bottom: 0;
	box-sizing: border-box;
}

.content {
	width: 600rpx;
	margin: 0 auto; /* 水平居中 */

	.title {
		text-align: center;
		font-size: 50rpx;
		font-weight: 500;
		color: #09b4f1;
		font-weight: bolder;
	}

	.login {
		background-color: #09b4f1;
		color: #ffffff;
		border-radius: 50rem !important;
		font-size: 26rpx;
		padding: 8rpx 0;
		margin-top: 40rpx;
		&::after {
			border: none;
		}
	}
	
	.login1 {
		background-color: #5ac725;
		color: #ffffff;
		border-radius: 50rem !important;
		font-size: 26rpx;
		padding: 8rpx 0;
		margin-top: 40rpx;
		&::after {
			border: none;
		}
	}
}
</style>